package com.yanqu.road.logic.xlogic.rank.season.stage;

import com.yanqu.road.entity.rank.xrank.XSeasonStageUserRank;
import com.yanqu.road.logic.bussiness.rank.XRankBussiness;
import com.yanqu.road.logic.xlogic.rank.base.AbstractXRankListModel;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class XSeasonStageUserRankListModel extends AbstractXRankListModel {

    private int systemId;
    private int seasonId;
    private int conditionType;
    private int stage;
    private String rangeKey;
    private int maxRank;
    private Comparator<XSeasonStageUserRank> rankSort;

    private LinkedList<XSeasonStageUserRank> rankList = new LinkedList<>();
    private int rankCapacity;

    private long allSyncTime;

    public XSeasonStageUserRankListModel(String dbName, String tableName,
                                         int systemId, int seasonId, int conditionType, int stage, String rangeKey, int maxRank, Comparator<XSeasonStageUserRank> rankSort) {
        super(dbName, tableName);

        this.systemId = systemId;
        this.seasonId = seasonId;
        this.conditionType = conditionType;
        this.stage = stage;
        this.rangeKey = rangeKey;
        this.maxRank = maxRank;
        this.rankSort = rankSort;

        this.rankCapacity = calcRankCapacity(maxRank);
    }

    public XSeasonStageUserRankListModel(String dbName, String tableName,
                                         int systemId, int seasonId, int conditionType, int stage, String rangeKey, LinkedList<XSeasonStageUserRank> rankList, int maxRank, Comparator<XSeasonStageUserRank> rankSort) {
        super(dbName, tableName);

        this.systemId = systemId;
        this.seasonId = seasonId;
        this.conditionType = conditionType;
        this.stage = stage;
        this.rangeKey = rangeKey;
        this.rankList = rankList;
        this.maxRank = maxRank;
        this.rankSort = rankSort;
        this.rankCapacity = calcRankCapacity(maxRank);
    }

    private int calcRankCapacity(int maxRank) {
        return maxRank + 20;
    }

    public void setMaxRank(int maxRank) {
        this.maxRank = maxRank;
        this.rankCapacity = calcRankCapacity(maxRank);
    }

    /**
     * 获取排名
     *
     * @param rankList 超出这个的长度排名为-1
     */
    private XGetSeasonStageUserRankResult getMyRankResult(long userId, List<XSeasonStageUserRank> rankList) {
        int rank = 0;
        for (XSeasonStageUserRank userRank : rankList) {
            rank++;
            if (userRank.getUserId() == userId) {
                return new XGetSeasonStageUserRankResult(userRank, rank);
            }
        }
        return new XGetSeasonStageUserRankResult();
    }

    public int getMyRank(long userId) {
        return getMyRankResult(userId, getRankList()).getMyRank();
    }

    public void initByRankList(LinkedList<XSeasonStageUserRank> rankList) {
        synchronized (this) {
            this.rankList = rankList;
            this.rankList.sort(rankSort);
            // 去掉多余的
            while (this.rankList.size() > rankCapacity) {
                this.rankList.removeLast();
            }
        }
    }

    public List<XSeasonStageUserRank> getRankList() {
        return getRankList(maxRank);
    }

    public List<XSeasonStageUserRank> getRankList(int topCount) {
        List<XSeasonStageUserRank> list = new LinkedList<>();
        for (XSeasonStageUserRank rank : new LinkedList<>(rankList)) {
            if (list.size() >= topCount) {
                break;
            }
            list.add(rank);
        }
        return list;
    }

    public List<XSeasonStageUserRank> getAllRankList() {
        return new LinkedList<>(rankList);
    }

    @Override
    protected void addAllDbRank(int maxRank) {
        super.addAllDbRank(maxRank);
    }

    public int getSystemId() {
        return systemId;
    }

    public int getSeasonId() {
        return seasonId;
    }

    public String getRangeKey() {
        return rangeKey;
    }

    public int getConditionType() {
        return conditionType;
    }

    public int getStage() {
        return stage;
    }

    public int getMaxRank() {
        return maxRank;
    }

    public Comparator<XSeasonStageUserRank> getRankSort() {
        return rankSort;
    }

    @Override
    public void save() {
        List<XSeasonStageUserRank> tmpRankList;
        Set<Integer> dbRankSet;
        synchronized (this) {
            tmpRankList = new LinkedList<>(rankList);
            dbRankSet = getAndClearAllDbRank();
        }
        // 根据dbName和tableName入库
        XRankBussiness.replaceXSeasonStageUserRank(dbName, tableName, tmpRankList, dbRankSet);
    }

}
